<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">Boke家纺</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.pwd"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024 robots</span>
    </div>
  </div>
</template>

<script>
import JSEncrypt from 'jsencrypt';
import {ajax} from '@/config/user'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        pwd: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      //公钥
      publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQGVu9dhNTWz+kw5vJifAPnBoppjo7MbqQRlbb93JZzu1lKNs/qzatPE+WFZXOSLFBwsx3CX+UUS8GVQ7yQIDaPImAvQvuRk6/a+cIzh7gDhNrfF90IAaepAbwq4c9n9Z+bkSb/KPbZ6WPmMunhPyuEJxACa6RFEenAt4mojFOBwIDAQAB',
      //私钥
      privateKey: 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANAZW712E1NbP6TDm8mJ8A+cGimmOjsxupBGVtv3clnO7WUo2z+rNq08T5YVlc5IsUHCzHcJf5RRLwZVDvJAgNo8iYC9C+5GTr9r5wjOHuAOE2t8X3QgBp6kBvCrhz2f1n5uRJv8o9tnpY+Yy6eE/K4QnEAJrpEUR6cC3iaiMU4HAgMBAAECgYAD4fuknofGlGGs0O5R/yR4uUobraHq6aCetIR5kjWSy2bOh/0FqrIlRVM1OUd6rXLxOsTWq7/mCowjROieFVBruV2Lxk2+PV5lQtKdwpv7xE+T3Oc42Kcm/UcrZjOI6R4sOEceMwq5G1kAQPNgxZM8BJTK5jo3B4VQDML70lvU8QJBAPdtSTnKwMPsyUU9soCHr4BHlsRL78MJtn9k6R4srI9KGwzgBVDR2198QZCm/ucCiJnXkZNEaHKUJ7eOOFejccMCQQDXTzp5pDb9mhq+F9assRItdmCiqY1EHeRzvG8bBH+CdlXNuf1OQS29hkImiPTKW/VvOrMFtzCPCezlrxt7csptAkAkiMmQNAUEqRkLFVIfMmnSqj9z2rabePqukurtHrGYMtwNqaJ/TY1z6vgPcOkgmTosZS1Erowxpvi6XNCq+CJbAkEAxGfimKoC+84CzRsj2j5grZYJyvhOBDAcgLMIyyWMR285EES0XBk652M8nrhaTOob9o4eVn1oT2MnddIJiNAK9QJAHLg9OXkX/UMAP1Q0EFfvCGidrjkAK5kqTTlkRGdZdf/nOVMZlzEhGsBn1cK+nDUvPOC5oKkUWglTcYceamUFPw==',
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        pwd: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    async handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const pms = {
            userName: this.loginForm.username,
            pwd: this.RSAgetPass(this.loginForm.pwd)
          }
          ajax.getlogin(pms).then(res => {
            var {data} = res
            if (data.code == '00000') {
              localStorage.setItem('Authorization', 'Bearer ' + data.data.token)
              localStorage.setItem('userType', data.data.userType)
              this.$router.push('/')
            } else {
              this.$message.error(data.msg)
              this.loading = false;
            }
          })
        } else {
          this.loading = false;
          this.$message.warning('请检查您的账号密码是否输入正确')
        }
      });
    },
    //加密方法
    RSAgetPass(pas) {
      const rsa = new JSEncrypt();
      rsa.setPublicKey(this.publicKey);
      console.log('加密：' + rsa.encrypt(pas))
      return rsa.encrypt(pas)
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url(../../assets/login-background.jpg);
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: orangered;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
